/*************************************************************************************** 
Firm-embedded productivity and cross-country income differences
Alviarez, Cravino and Ramondo
Journal of Political Economy (2022)

Program: model_04_bydecile.do
Date: October 2022

Description: Constructing the DA dummies (aggregate country effects) keeping MNEs with MNE-sector FE belonging to: 2nd-9th deciles, 3rd-8th deciles, 4th-7th deciles and 5th-6th deciles. 

*****************************************************************************************/

*-------------------------------------------------------------------------------
global typeden=1
include "set_directories.do"
set memory 64g
global lf "LF"

*Log
cap log close
log using "${clogs}/model_04_bydecile.log", replace
*-------------------------------------------------------------------------------

*Prepare the data for the regressions
*-----------------------------------------------------------
global conditionforage ""
include "${cmain}/prep_01_forreg.do"


*-------------------------------------------------------------------------------
*Keeping MNEs with MNE-sector FE belonging to: 2nd-9th deciles, 3rd-8th deciles, 4th-7th deciles and 5th-6th deciles. 
*-------------------------------------------------------------------------------
foreach decile in "base_woparent_decile_2_9"  "base_woparent_decile_3_8"  "base_woparent_decile_4_7"  "base_woparent_decile_5_6" {
display "`decile'"

local yy_start=substr("`decile'",-3,1)
local yy_end=substr("`decile'",-1,1)
local yy_start0=`yy_start'-1
display "`yy_start'-`yy_end'-`yy_start0'"

local heading "base_woparent_decile_`yy_start'_`yy_end'"  
local timeframe `" "2016" "' 
local varinterest sales 


if "`heading'"=="base_woparent_decile_`yy_start'_`yy_end'" {

local ending ""
local zz naics  
local var0 "_`varinterest'" 
local name "_base" 
local name1 "_woparent_decile_`yy_start'_`yy_end'"
local samp=1 
local title "`zz'`var0'_s`samp'`name'`name1'`ending'"
display "`title'"

global ctryrel FR
local period "`timeframe'"
global nonmarket `" drop if sector1=="" "' 
global cont_firms `" keep if years_after2010>=0 "'
global cont_firms2 `" keep if years_allperiod>=0 "'
local absFEreg "guoFE" 
global ind_var dist_sec* lang_sec* 
global guoFEonly=0 
global LHS "ln(sin`var0'`denominator')" 
global numctry `" keep if num_ctry>=0 "'
global pctiles  `" keep if sin_sales>p`yy_start0'0_sales & sin_sales<=p`yy_end'0_sales "'  
global other_restr `" drop if isocode==hq "'
global other_restr2  ""
}
*
display "`title'"
local affparent "0" /*"0" is the default, "1" if keeping even one affiliate after parents are dropped*/ 
include "${cmain}/reg_01_naics.do"

*Aggregating the sectoral estimates (Aggregate)
local weightsused_FR=0
local typeused "agg" 
include "${cmain}/reg_02_agg.do"


*Erase intermediate files 
*---------------------------------------------------------
erase "${data}/ctte_naics_sales_s1_base_woparent_decile_`yy_start'_`yy_end'.dta"
erase "${data}/firmFE_naics_sales_s1_base_woparent_decile_`yy_start'_`yy_end'.dta"
erase "${data}/residual_naics_sales_s1_base_woparent_decile_`yy_start'_`yy_end'.dta"
erase "${data}/gravity_naics_sales_s1_base_woparent_decile_`yy_start'_`yy_end'.dta"
erase "${data}/gravitycoef_naics_sales_s1_base_woparent_decile_`yy_start'_`yy_end'.dta"
erase "${data}/reg0_naics_sales_s1_base_woparent_decile_`yy_start'_`yy_end'_2016.dta"
erase "${data}/pre_reg0_naics_sales_s1_base_woparent_decile_`yy_start'_`yy_end'_2016.dta"

}


log close 


